package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.service.TokenDubboService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;

import java.util.Optional;

@Service
public class TokenDubboServiceImpl implements TokenDubboService {

    @Autowired
    private LagouTokenDao tokenDao;

    /**
     * 通过token获取
     *
     * @param token
     * @return
     */
    @Override
    public String findEmailByToken(String token) {
        LagouToken lagouToken = LagouToken.builder().token(token).build();

        Example<? extends LagouToken> example = Example.of(lagouToken);
        Optional<? extends LagouToken> tokenOptional = tokenDao.findOne(example);
        if (tokenOptional.isPresent()) {
            return tokenOptional.get().getEmail();
        }
        return "";
    }
}
